package com.atguigu02.reference;

import com.atguigu02.reference.data.Employee;
import jdk.nio.mapmode.ExtendedMapMode;
import org.junit.Test;

import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 方法引用的使用
 *
 * Created by shkstart.
 */
public class MethodRefTest {

	// 情况一：对象 :: 实例方法
	//Consumer中的void accept(T t)
	//PrintStream中的void println(T t)
	@Test
	public void test1() {
		//方法一
		Consumer con1 = s -> System.out.println(s);
		con1.accept("world");

		//方法2
		Consumer con2 = System.out ::println;
		con2.accept("hello");
	}
	
	//Supplier中的T get()
	//Employee中的String getName()
	@Test
	public void test2() {

		Employee emp = new Employee(1,"z",12,1000);

		//一
		Supplier sup = () -> emp.getName();
		System.out.println(sup.get());

		//二
		Supplier sup2 = emp::getName;
		System.out.println(sup.get());
	}

	// 情况二：类 :: 静态方法
	//Comparator中的int compare(T t1,T t2)
	//Integer中的int compare(T t1,T t2)
	@Test
	public void test3() {

		//一
		Comparator<Integer> com1 = (o1, o2) -> Integer.compare(o1,o2) ;
		System.out.println(com1.compare(12, 13));

		//二
		Comparator<Integer> com  = Integer ::compare;
		System.out.println(com.compare(12, 13));
	}
	
	//Function中的R apply(T t)
	//Math中的Long round(Double d)
	@Test
	public void test4() {

		//一
		Function<Double,Long> fun = aDouble -> Math.round(aDouble);

		//二
		Function<Double,Long> fun1 = Math::round;

	}

	// 情况三：类 :: 实例方法 
	// Comparator中的int comapre(T t1,T t2)
	// String中的int t1.compareTo(t2)
	@Test
	public void test5() {
		Comparator<String> com = String ::compareTo;
		System.out.println(com.compare("abc", "bac"));
	}

	//BiPredicate中的boolean test(T t1, T t2);
	//String中的boolean t1.equals(t2)
	@Test
	public void test6() {
		BiPredicate<String,String> pre = String ::equals;
		System.out.println(pre.test("abc", "bac"));

	}
	
	// Function中的R apply(T t)
	// Employee中的String getName();
	@Test
	public void test7() {
		Employee emp = new Employee(1,"z",22,2000);
		Function<Employee,String> fun = Employee ::getName;
		System.out.println(fun.apply(emp));
	}

}
